package com.dream.net.socket.base;

import com.alibaba.fastjson.JSONObject;
import com.dream.common.constans.RedisKeysDefine;
import com.dream.dto.socket.SocketBean;
import com.dream.service.basic.RedisCacheService;
import com.dream.web.websocket.ConsoleWebSocket;
import io.netty.channel.ChannelHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


@Component
@ChannelHandler.Sharable
public class SocketController extends BaseSocketController {

    private Logger logger = LoggerFactory.getLogger(SocketController.class);

    @Resource
    private RedisCacheService redisCacheService;
    @Resource
    private ConsoleWebSocket consoleWebSocket;
    /**
     * 接收到的指令
     */
    public void commandReceived(SocketBean socketBean,String sourceMsg) {
        switch (socketBean.command) {
            case 0:
                logger.info("-发送心跳包 {}",socketBean.data);
                break;
                //聊天
            case 1:
                logger.info("-app 发送聊天command {}",sourceMsg);
                JSONObject jsonObject = JSONObject.parseObject(socketBean.data);
                String account = jsonObject.getString("userName");
                redisCacheService.zSetOperations.add(RedisKeysDefine.IM_MSG.concat(account),
                        socketBean.data,System.currentTimeMillis());

                consoleWebSocket.sendMessageToUser(account, sourceMsg);
                break;
            default:
                break;
        }
    }



}
